package demo;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

	public static void main(String[] args) {
		for (int i = 2021; i < 2024; i++) {
			getWeeks(i).forEach(System.out::println);
			System.out.println("==================================");
		}
	}

	public static List<Map<String, String>> getWeeks(int inputYear) {
		List<Map<String, String>> result = new ArrayList<>();
		SimpleDateFormat fmt1 = new SimpleDateFormat("yyyy年MM月dd日");
		SimpleDateFormat fmt2 = new SimpleDateFormat("yyyy-MM-dd");

		Date now = new Date();
		Calendar nowCal = Calendar.getInstance();
		nowCal.setTime(now);

		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, inputYear);
		cal.setFirstDayOfWeek(Calendar.FRIDAY);

		int weekCount = cal.get(Calendar.WEEK_OF_YEAR);

		if (cal.get(Calendar.MONTH) >= 11 && weekCount <= 1 && inputYear <= nowCal.get(Calendar.YEAR)) {
			weekCount = 52;
		}

		String startYear = "";

		for (int i = 1; i <= weekCount; i++) {
			cal.set(Calendar.WEEK_OF_YEAR, i);
			cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
			Date firstDayOfWeek = cal.getTime();

			cal.add(Calendar.DAY_OF_WEEK, 6);
			Date lastDayOfWeek = cal.getTime();

			if (i == 1) {
				startYear = fmt2.format(firstDayOfWeek);
			}

			Map<String, String> item = new HashMap<>();
			item.put("value", i + "");
			item.put("key", "第" + (i) + "周 " + fmt1.format(firstDayOfWeek) + "-" + fmt1.format(lastDayOfWeek));
			item.put("beginDate", fmt2.format(firstDayOfWeek));
			item.put("endDate", fmt2.format(lastDayOfWeek));
			item.put("startYear", startYear);
			result.add(item);
		}

		return result;
	}

}
